package com.gxa.weixing.pc.controller;

import com.gxa.weixing.core.common.JsonResult;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.User;
import com.gxa.weixing.pc.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "用户相关的api")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService = null;

    @ApiOperation("获取用户列表")
    @PostMapping("/getUserList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code",value = "code",required = false,dataType = "String",paramType = "query",dataTypeClass = String.class),
            @ApiImplicitParam(name = "nickName",value = "nickName",required = false,dataType = "String",paramType = "query",dataTypeClass = String.class),
            @ApiImplicitParam(name = "startTime",value = "startTime",required = false,dataType = "Date",paramType = "query",dataTypeClass = Date.class),
            @ApiImplicitParam(name = "endTime",value = "endTime",required = false,dataType = "Date",paramType = "query",dataTypeClass = Date.class),
            @ApiImplicitParam(name = "page",value = "page",required = false,dataType = "Integer",paramType = "query",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "size",value = "size",required = false,dataType = "Integer",paramType = "query",dataTypeClass = Integer.class)
    })
    public Map<String,Object> getUserList(String code, String nickName, Date startTime,Date endTime,Integer page,Integer size) throws BusinessException {
        List<User> byPage = userService.getUserListByPage(code, nickName, startTime, endTime, page, size);
        Integer count = userService.getUserListPageCount(code, nickName, startTime, endTime);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(byPage);
        jsonResult.put("count",count);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "userId",value = "userId",required = true,dataType = "Long",paramType = "query",dataTypeClass = Long.class)
    @PostMapping("/delete")
    public Map<String,Object> delete(@RequestParam Long id) throws BusinessException {
        userService.deleteById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("获取用户提现申请列表")
    @PostMapping("/getWithdraw")
    public Map<String,Object> getWithdraw(@RequestBody Map<String,Object> map) throws BusinessException, ParseException {

        List<Map<String, Object>> page = userService.getWithdrawByPage(map);
        Integer count = userService.getWithdrawByPageCount(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(page);
        jsonResult.put("count",count);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("审核操作")
    @PostMapping("/check")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,dataType = "Long",paramType = "query",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status",value = "status",required = true,dataType = "Integer",paramType = "query",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "remark",value = "remark",required = false,dataType = "String",paramType = "query",dataTypeClass = String.class)
    })
    public Map<String,Object> check(Long id,Integer status,String remark ) throws BusinessException {
        userService.updateLogStatus(id, status, remark);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

}
